//
// C++ Implementation: qtactivityview
//
// Description:
//
//
// Author: Willi Ballenthin <wilbal1087@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "activityviewwidget.h"
#include <QDebug>

/**
* Constructor.
* @param interface A reference to a database interface from which to get the model
* @param parent The parent widget.
* of activities.
*/
ActivityViewWidget::ActivityViewWidget ( ActivityDbInterface* interface, QWidget *parent )
        : ActivityWidget ( interface, parent )
{
    activity = Activity(interface);
    activities = interface->getModel();
    setMinimumSize(QSize(200, 200));

    ui.setupUi ( this );

    connect(ui.cb_chooseDate, SIGNAL(activated(int)),
             this, SLOT(handleCbInput(int)));
    connect(ui.pb_later, SIGNAL(clicked()), this, SLOT(nextActivity()));
    connect(ui.pb_earlier, SIGNAL(clicked()), this, SLOT(prevActivity()));
    connect(ui.pb_edit, SIGNAL(clicked()), this, SLOT(editActivity()));
    connect(ui.pb_new, SIGNAL(clicked()), this, SLOT(newActivity()));

    setActivities(activities);
}


/**
*   A slot to handle combo box input. Translates the index
*   to an activity id (without relying on the index of the CB).
*   Sets the current view to the specified activity.
*   @param index The index of the combobox selected.
*/
void ActivityViewWidget::handleCbInput(int index )
{
    int id = ui.cb_chooseDate->itemData(index).toInt();
    showActivity(id);
}


/**
* Updates the vector of activities to be displayed in this widget.
* @param vector A vector of activities to be display-able.
*/
void ActivityViewWidget::setActivities(QVector<Activity>* vector)
{
    index = 0;
    this->activities = vector;

    activity = activities->value(index);

    updateUi();
}

/**
* Checks to see if the current activity is on an extreme end
* of the vector, and if so, disable the respective fore/back
* button.
*/
void ActivityViewWidget::checkButtons()
{
    if ( index <= 0 )
    {
        ui.pb_earlier->setEnabled ( false );
    }
    else
    {
        ui.pb_earlier->setEnabled ( true );
    }

    if (index + 1  >=  activities->size())
    {
        ui.pb_later->setEnabled ( false );
    }
    else
    {
        ui.pb_later->setEnabled ( true );
    }
}


/**
* Updates the displayed data regarding an activity.
*/
void ActivityViewWidget::updateUi()
{
    int i;
    ui.cb_chooseDate->clear();
    for( i = 0; i < activities->size(); i++)
    {
        Activity act = activities->value(i);
        ui.cb_chooseDate->addItem(act.getDate().toString(), act.getId());
    }

    activity = activities->value(index);

    ui.le_date->setText(activity.getDate().toString());
    ui.te_time->setTime(activity.getTime());
    ui.te_duration->setTime(activity.getDuration());
    ui.dsb_distance->setValue(activity.getDistance());
    ui.sb_effort->setValue(activity.getEffort());
    ui.le_type->setText(activity.getType());
    ui.le_location->setText(activity.getLocation());
    ui.le_route->setText(activity.getRoute());
    ui.te_notes->setText(activity.getNotes());
    ui.le_splits->setText(activity.getSplits());


    // search combo box for specified id, then make sure that is
    // selected.  This approach separates the CB from relying on
    // its index vs model index.
    int curCbIndex = ui.cb_chooseDate->findData(activity.getId());
    ui.cb_chooseDate->setCurrentIndex(curCbIndex);

    checkButtons();
}

/**
* Changes the view to the next available activity.
* Depends on the ordering of the vector.
*/
void ActivityViewWidget::nextActivity()
{
    if (index + 1  >=  activities->size())
    {
        // do nothing, should never happen
    }
    else
    {
        index++;
    }
    updateUi();
}

/**
* Changes the view to the previous activity.
*/
void ActivityViewWidget::prevActivity()
{
    if ( index <= 0 )
    {
        // do nothing, should never happen
    }
    else
    {
        index--;
    }
    updateUi();
}

/**
* Selects and shows the activity that took place on a date.
* @param date The date on which the activity took place.
* TODO: distinguish among multiple activities on a date, or none at all.
* This could be done by passing the ID of the activity, instead of the index/date.
*/
void ActivityViewWidget::showActivity(QDate date)
{
    int i;
    for(i = 0; i < activities->size(); i++)
    {
        if(activities->value(i).getDate() == date)
        {
            index = i;
            updateUi();
            return;
        }
    }
    qWarning() << "date index does not exist within current model";
}


/**
* Overloaded function, selects activity by index in vector.
* @param id The ID of the Activity to display.
*/
void ActivityViewWidget::showActivity(int id)
{
    int i;
    for (i = 0; i < activities->size(); i++)
    {
        if(activities->value(i).getId() == id)
        {
            index = i;
            updateUi();
            return;
        }
    }

    qWarning() << "id specified does not exist in current model";
}

/**
* This function emits a signal that an activity needs to be edited.
*/
void ActivityViewWidget::editActivity()
{
    Activity editActivity = activities->value(index);

    // pass by value, to satisfy concurrent edits on the vector
    emit(needEdit(editActivity));
}

/**
* This function emits a signal that a new activity needs to be set up.
*/
void ActivityViewWidget::newActivity()
{
    Activity newActivity(interface);

    // pass by value, to satisfy concurrent edits on the vector
    emit(needEdit(newActivity));
}


